<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$return['params']=$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['cid'])){
		$params['cid']=intval($_RQ['cid']);
		$return['params']['pid']=$_RQ['pid'];
		$return['params']['cid']=$_RQ['cid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','cid','goodsid','createtime','auction_no','certificate_no');
	$list=pdo_getslice('jy_jdpm_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','mid','merchid','image','images','price'));
			$images=json_decode($goodsinfo['images'],true);
			$item['image']=$goodsinfo['image'];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('avatar','username','nickname'));
			$item['title'] = cfc('tools')->substr($goodsinfo['title'],0,18);
			$item['account']=$member['username']?$member['username']:'平台';
			$item['avatar'] = $member['avatar']?$member['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['nickname']=$member['nickname']?$member['nickname']:'平台发布';
			$member1=pdo_get('core_members',array('id'=>$item['mid']),array('avatar','username','nickname'));
			$item['account1']=$member1['username'];
			$item['avatar1'] = $member1['avatar']?$member1['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['nickname1']=$member1['nickname'];
			$group=	pdo_get('jy_jdpm_goods_group',array('id'=>$item['cid']),array('pid','title'));
			$p=pdo_getcolumn('jy_jdpm_goods_group',array('id'=>$group['pid']),'title');
			$item['pid']=$p;
			$item['group']=$p.'/'.$group['title'];
			$item['times']=pdo_count('jy_jdpm_auction',array('goodsid'=>$item['id']));
			$item['times']=$item['times'].'次';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('name'=>'title'),
				)
			),
			array('name'=>'group','title'=>'频道/分类','type'=>'string'),
			array('type'=>'img_string','title'=>'拍品编号/证书编号',
			'data'=>array(
				array('title'=>'拍品编号：','name'=>'auction_no'),
				array('title'=>'证书编号：','name'=>'certificate_no')
				)
			),
			array('name'=>'createtime','title'=>'发布时间','type'=>'string'),
			array('type'=>'img_string','title'=>'卖家账号','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'account'),
				array('title'=>'昵称：','name'=>'nickname')
				)
			),
			array('name'=>'times','title'=>'已发拍卖','type'=>'string'),
		),
		'action'=>array(	
			array('action'=>'auction','op'=>'edit','title'=>'添加拍卖','type'=>'href'),
			array('op'=>'zc','title'=>'添加专场拍','type'=>'href'),
		),
		),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'商品ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'商品名称','name'=>'title','help'=>''),
			//array('type'=>'group_select','width'=>'15','title'=>'分类','cid'=>'cid','pid'=>'pid','help'=>'','options'=>'cid','option_value'=>'cid','option_title'=>'pname','pid_title'=>'请选择频道','cid_title'=>'请选择子类'),
		),
	);
	$return['pid']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$c=array();
	foreach($return['pid'] as $v){
		$n=$v['name'];
		$son=pdo_getall('jy_jdpm_goods_group',array('pid'=>$v['cid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($son as $s){
			$d=$s['cid'];
			$c[$n][$d]=$s['name'];
		}
	}
	$return['cid']=$c;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	//$return['pcd']=cfc('tools')->pcd();
	$id=intval($_RQ['id']);
	if($id){
		$item=get_goodsinfo($id);
		$pname=pdo_getcolumn('jy_jdpm_goods_group',array('id'=>$item['pid']),'title');
		$item['cid']=array('title'=>$pname,'cid'=>$item['cid']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'desc'=>'商品设置',
		'edit'=>array(
			//array('type'=>'choose','title'=>'所属用户','name'=>'merchid','op'=>'get_member','choose_title'=>'选择会员'),
			array('type'=>'choose','title'=>'所属卖家','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
			array('name'=>'cid','title'=>'频道/分类','type'=>'goodgroup','options'=>'cid',),
			array('name'=>'auction_no','title'=>'拍品编号','type'=>'string'),
			array('name'=>'certificate_no','title'=>'证书编号','type'=>'string'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
		);
	//获取分类
	$return['pcate']=pdo_getall('jy_jdpm_goods_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0));
	$c=array();
	foreach($return['pcate'] as $v){
		$n=$v['title'];
		$son=pdo_getall('jy_jdpm_goods_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$v['id']));
		if($son){
			foreach($son as $s){
				$d=$s['id'];
				$c[$n][$d]=$s['title'];
			}
		}else{
			$c[$n][0]='';
		}
	}
	$return['cid']=$c;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	$data=array(
		'goodsid'=>intval($post['goodsid']),
		'paixu'=>intval($post['paixu']),
		'certificate_no'=>$post['certificate_no'],
		'auction_no'=>$post['auction_no']
	);
	if(!$post['goodsid']){
		exi('请选择商品','error');
	}
	if(!$post['cid']){
		exi('请选择分类','error');
	}
	if(!$post['certificate_no']){
		exi('请填写证书编号','error');
	}
	if(!$post['auction_no']){
		exi('请填写拍品编号','error');
	}
	if($post['mid']){
		pdo_update('jy_weishop_goods',array('mid'=>$post['mid']),array('id'=>$post['goodsid'],'uniacid'=>$_SESSION['uniacid']));
	}
	//获取分类
	$ccate = intval($post['cid']['cid']);
	if($ccate){
		$pcate = pdo_getcolumn('jy_jdpm_goods_group',array('id'=>$ccate),'pid');
		$data['cid']=$ccate;
		$data['pid']=$pcate;
	}else{
		exi('请选择子分类','error');
	}
	if(!$id){
		$ispai = pdo_get('jy_jdpm_goods',array('goodsid'=>$post['goodsid']),'id');
		if($ispai){
			exi('改商品已经参与拍卖！','error');
		}
		$data['createtime']=time();
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_jdpm_goods',$data);
		exi('添加成功','','list');
	}else{
		pdo_update('jy_jdpm_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功','','list');
	}
}
if($_RQ['op']=='zc'){
		$id=intval($_RQ['id']);
		$goodsinfo=get_goodsinfo($id);
		$spec=pdo_get('jy_jdpm_special_auction',array('uniacid'=>$_SESSION['uniacid']));
		$r=pdo_get('jy_jdpm_auction',array('goodsid'=>$id,'endstatus'=>array(2,3,4),'uniacid'=>$_SESSION['uniacid']),array('id'));
		$item=pdo_get('jy_jdpm_auction',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if(!$r && $item){
			exi('该商品正在拍卖或已成交，不允许再次拍卖！只有流拍和撤拍的商品才能重新发布拍卖','error','list');
		}
		if(!$spec){
			exi('没有可供发布的专场，请先添加专场','error','/special/edit');
		}else{
			if(!$item['id']){
				$item['goodsid']=$id;
				$item['pname']=$goodsinfo['title'];
				$item['price']=$goodsinfo['price'];
				$item['onset']=$goodsinfo['price'];
				$item['stepsize']=10;
				$item['steptime']=300;
				$item['deferred']=5;
			}
			$return['item']=$item;
			$return['special']=pdo_getall('jy_jdpm_special_auction',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
			$htmls=array(
			'type'=>'edit',
			'post'=>'post1',
			'desc'=>'拍卖设置',
			'edit'=>array(
				array('type'=>'select','title'=>'选择专场','name'=>'sid','options'=>'special','option_title'=>'title','option_value'=>'id'),
				array('type'=>'string','title'=>'拍卖标题','name'=>'pname'),
				array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
				array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
				array('name'=>'onset','title'=>'起拍价','type'=>'string'),
				array('name'=>'price','title'=>'保留价','type'=>'string'),
				array('name'=>'steptime','title'=>'触发延时时间段(秒)','type'=>'string','help'=>'设置最后多少秒内出价触发延时'),
				array('name'=>'deferred','title'=>'延时时间(分)','type'=>'string'),
				array('name'=>'stepsize','title'=>'定额浮动(%)','type'=>'string','help'=>'每次出价固定加价幅度'),
				
			)
			);
		
		$return['htmls']=$htmls;
		exi($return);
	}	
}
if($_RQ['op']=='post1'){
	$post=get_postdata();
	$data=array(
		'goodsid'=>$post['goodsid'],
		'pname'=>$post['pname'],
		'endstatus'=>0,
		'starttime'=>strtotime($post['starttime']),
		'endtime'=>strtotime($post['endtime']),
		'sid'=>$post['sid'],
		'onset'=>$post['onset'],
		'price'=>$post['price'],
		'pattern'=>2,
		'succtype'=>0,
		'steptime'=>$post['steptime'],
		'deferred'=>$post['deferred'],
		'stepsize'=>$post['stepsize'],
	);
	if(empty($data['sid'])){
		exi('未选择专场！','error');
	}
	if(empty($data['starttime'])){
		exi('开始时间未设置！','error');
	}
	if(empty($data['endtime'])){
		exi('结束时间未设置！','error');
	}
	if(empty($data['onset'])){
		exi('起拍价未设置！','error');
	}
	if(empty($data['price'])){
		exi('保留价未设置！','error');
	}
	if($post['id']){
		pdo_update('jy_jdpm_auction',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_jdpm_auction',$data);
		pdo_update('jy_jdpm_special_auction',array('bcount +='=>1),array('id'=>$data['sid'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('添加成功','','list');
}
if($_RQ['op']=='groups'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'pid <'=>1
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	
	$list=pdo_getslice('jy_jdpm_goods_group',$params,array($pindex,$psize),$total,$fields);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'groups','title'=>'频道'),
			array('op'=>'groups2','title'=>'分类'),
			array('op'=>'groupsedit','title'=>'添加频道/分类'),
		),
		'list'=>array('edit'=>false,'delall'=>false,'del_one'=>false,'data'=>'list','list'=>array(
			array('name'=>'id','title'=>'CID','type'=>'string'),
			array('name'=>'title','title'=>'频道标题','type'=>'string'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'hot','title'=>'推荐','type'=>'radio','options'=>'hot'),

		),
		'action'=>array(
			array('op'=>'groupsedit','title'=>'编辑','type'=>'href'),
			array('op'=>'del2','title'=>'删除','type'=>'post','message'=>'是否删除？'),		
			),),
	);
	$return['hot']=array('1'=>'是','0'=>'否');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='groups2'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'pid >'=>0
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	
	$list=pdo_getslice('jy_jdpm_goods_group',$params,array($pindex,$psize),$total,$fields,'id desc');
	foreach($list as &$v){
		$v['pname']=pdo_getcolumn('jy_jdpm_goods_group',array('id'=>$v['pid']),'title');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'groups','title'=>'频道'),
			array('op'=>'groups2','title'=>'分类'),
			array('op'=>'groupsedit','title'=>'添加频道/分类'),
		),
		'list'=>array('edit'=>false,'del_one'=>false,'delall'=>false,'data'=>'list','list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'分类标题','type'=>'string'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'pname','title'=>'上级频道','type'=>'string'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'hot','title'=>'推荐','type'=>'radio','options'=>'hot'),
		),
		'action'=>array(
			array('op'=>'groupsedit','title'=>'编辑','type'=>'href'),
			array('op'=>'del2','title'=>'删除','show'=>array('pid >'=>0),'type'=>'post','message'=>'是否删除？'),		
			),),
	);
	$return['hot']=array('1'=>'是','0'=>'否');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='groupsedit'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jdpm_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['sort']=intval($item['sort']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'商品分类',
		'tabs'=>array(
			array('op'=>'groups','title'=>'频道'),
			array('op'=>'groups2','title'=>'分类'),
			array('op'=>'groupsedit','title'=>'添加频道/分类'),
		),
		'edit'=>array(
			array('type'=>'select','title'=>'选择父类','name'=>'pid','options'=>'groups','option_title'=>'title','option_value'=>'id','help'=>'不选择父类则为频道'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'image','title'=>'图标','type'=>'image'),
			array('name'=>'hot','title'=>'推荐','type'=>'radio','options'=>'hot'),
			//array('name'=>'modelno','title'=>'显示效果','type'=>'radio','options'=>'modelno','help'=>'注意：分类必须为顶级分类（频道）且【推荐】为是且符合显示效果商品数量要求，才会有效否则使用默认效果'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
	);
	$return['groups']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['hot']=array('1'=>'是','0'=>'否');
	//$return['modelno']=array('0'=>'默认效果','1'=>'效果一（商品最低7个有效）','2'=>'效果二（商品最低9个有效）');
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($post['id']);
	$data=array('pid'=>$post['pid'],'title'=>$post['title'],'hot'=>$post['hot'],'image'=>$post['image'],'paixu'=>$post['paixu']);
	if(!$id){
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_jdpm_goods_group',$data);
		exi('添加成功','','groups');
	}else{
		pdo_update('jy_jdpm_goods_group',$data,array('id'=>$id));
		exi('修改成功','','groups');
	}
	
	
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jdpm_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='del2'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jdpm_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='get_member'){
	//会员列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array('uniacid'=>$_SESSION['uniacid']);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$fields=array('username','nickname','mobile');
	if($_RQ['find']){
		$field=in_array($_RQ['field'],$fields)?$_RQ['field']:'nickname';
		$params[$field.' like']='%'.$_RQ['find'].'%';
		$return['params']['find']=$_RQ['find'];
		$return['params']['field']=$field;
	}
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,array('id','username','mobile','nickname','avatar'),'','id desc');
	if($list){
		foreach($list as &$item){
			$item['mid']= pdo_getcolumn('jy_jdpm_member', array('mid' => $item['id'],'uniacid'=>$_SESSION['uniacid']),'mid');
			$item['username']=cfc('tools')->substr($item['username'],0,6).'****';
			$item['mobile']=cfc('tools')->substr($item['mobile'],0,6).'****';
			$item['avatar'] = $item['avatar']?$item['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员列表'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
		    array('name'=>'mid','title'=>'会员id','type'=>'string'),
			array('name'=>'username','title'=>'会员账号','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'mid','title_field'=>'nickname','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'find','select_name'=>'field','select_title'=>'筛选','is_radio'=>true,'options'=>'fields'),
	);
	$return['fields']=array(
		'username'=>'会员账号',
		'nickname'=>'会员昵称',
		'mobile'=>'会员手机',
	);
	exi($return);
}